package com.lxm.ymcc.mq;

import com.alibaba.fastjson.JSON;
import com.lxm.ymcc.constents.MqConstants;
import com.lxm.ymcc.domain.MediaFile;
import com.lxm.ymcc.result.JSONResult;
import com.lxm.ymcc.service.IMediaFileService;
import com.mysql.cj.x.protobuf.MysqlxDatatypes;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = MqConstants.MQ_TO_PIC,
        selectorExpression=MqConstants.MQ_TAGES	//tags
        ,consumerGroup = "service-consumer"
        ,messageModel = MessageModel.CLUSTERING )
@Slf4j
public class MqMessageConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    public void onMessage(MessageExt messageExt) {
        // 将数据拉取出来消费
        byte[] body = messageExt.getBody();
        // 转化成字符串
        String mediaStr = new String(body);
        // 转换为mediafile
        MediaFile mediaFile = JSON.parseObject(mediaStr, MediaFile.class);
        // 切片推流
        JSONResult jsonResult = mediaFileService.handleFile2m3u8(mediaFile);
        log.info("视频推送 {}" ,jsonResult.isSuccess());



    }
}
